//
//  NSString+category.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/1.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "NSString+category.h"
#import <CoreText/CoreText.h>

@implementation NSString (category)

#pragma mark 校验身份证
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

#pragma mark 给字符串加下划线
+(NSMutableAttributedString *)makeLineStyle:(NSString *)string color:(UIColor *)color fontSize:(CGFloat)fontSize
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
     NSRange strRange = {0,[str length]};
    
    [str addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:fontSize]
     
                          range:strRange];
    [str addAttribute:NSForegroundColorAttributeName
     
                          value:color
     
                          range:strRange];
   
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:strRange];
    
    return str;

}



#pragma mark - 校验身份证号
+ (BOOL) isValidIdentifier:(NSString*)value
{
    
    const int factor[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };//加权因子
    const int checktable[] = { 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };//校验值对应表
    
    const int LENGTH = 18;
    const char *str = [[value lowercaseString] UTF8String];
    NSInteger i;
    NSInteger length = strlen(str);
    BOOL result = TRUE;
    /*
     * identifier length is invalid
     */
    if (15 != length && LENGTH != length)
    {
        result = FALSE;
    }
    else
    {
        for (i = 1; i < length - 1; i++)
        {
            if(!(str[i] >= '0' && str[i] <= '9'))
            {
                result = FALSE;
                break;
            }
        }
        if (result)
        {
            if(LENGTH == length)
            {
                if (!((str[i] >= '0' && str[i] <= '9')||str[i] == 'X'||str[i] == 'x'))
                {
                    result = FALSE;
                }
            }
        }
        /*
         * check sum for second generation identifier
         */
        if (result && length == LENGTH)
        {
            int i;
            int *ids = malloc(sizeof(int)*LENGTH);
            for (i = 0; i < LENGTH; i++)
            {
                ids[i] = str[i] - 48;
            }
            int checksum = 0;
            for (i = 0; i < LENGTH - 1; i ++ )
            {
                checksum += ids[i] * factor[i];
            }
            if (ids[17] == checktable[checksum%11]||
                (str[17] == 'x' && checktable[checksum % 11] == 10))
            {
                result  = TRUE;
            }
            else
            {
                result  = FALSE;
            }
            free(ids);
            ids = NULL;
        }
    }
    return result;
}


#pragma mark - 校验护照
+ (BOOL) isValidPassport:(NSString*)value
{
    const char *str = [value UTF8String];
    char first = str[0];
    NSInteger length = strlen(str);
    if (!(first == 'P' || first == 'G'))
    {
        return FALSE;
    }
    if (first == 'P')
    {
        if (length != 8)
        {
            return FALSE;
        }
    }
    if (first == 'G')
    {
        if (length != 9)
        {
            return FALSE;
        }
    }
    BOOL result = TRUE;
    for (NSInteger i = 1; i < length; i++)
    {
        if (!(str[i] >= '0' && str[i] <= '9'))
        {
            result = FALSE;
            break;
        }
    }
    return result;
}
+(NSMutableAttributedString *)makecolor:(NSString *)string color:(UIColor *)color;
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSForegroundColorAttributeName
     
                value:color
     
                range:strRange];
    return str;

}
#pragma mark 获取文字的所在的cgsize
+(CGSize)stringSizeWithMaxSize:(CGSize)maxSize WithFontSize:(CGFloat)fontSize withString:(NSString *)string{
    
     NSDictionary *attrs =@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]};
    return [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

#pragma mark 校验邮箱
+(BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
@end
